<?php
/**
 * Created by PhpStorm.
 * User: WangXiaoHong
 * Date: 15-8-31
 * Time: 上午11:14
 */
/*接受数据*/
$title     = $_GET["name"];
$endtime   = $_GET['endtime'];
$begintiem = $_GET['begintiem'];
/*$target标志位，分为三个值，1代表搜索全部，2代表搜索显示一级分销商，3代表搜索显示末级分销商*/
$target    = $_GET['target'];
strtotime($begintiem)>time()?exit('{"status":"fail","msg":"请选择正确时间"}'):'';
$begintiem==date('Y-m-d',time())?exit('{"status":"fail","msg":"当天时间没有数据"}'):'';
$p_type    = array('A'=>'景区','B'=>'路线','C'=>'酒店','F'=>'套票','G'=>'餐饮','H'=>'演出');
/*连接数据库*/
require_once("/var/www/html/new/conf/le.je");
$le        = new go_sql();
$le->connect();

/*判断是否有输入产品名称，没有输入则搜索全部产品*/
/*输入产品名称，显示全部供应商或者一级供应商*/
if(!empty($title)&& $target!=3)
{
    /*通过输入的产品名称搜索数据库得到产品的信息*/
    $sql= "SELECT id,p_type,jtype,apply_did,title FROM uu_land WHERE title='$title' limit 1";
    $GLOBALS['le']->query($sql);
    /*搜索显示景区的信息，键值为景区id即lid*/
    while($row=$GLOBALS['le']->fetch_assoc())
    {
        $id=$row['id'];
        $r_lid[$row['id']]['id']       =$row['id'];
        $r_lid[$row['id']]['p_type']   =$p_type[$row['p_type']];
        $r_lid[$row['id']]['jtype']    =$row['jtype'];
        $r_lid[$row['id']]['apply_did']=$row['apply_did'];
        $r_lid[$row['id']]['title']    =$row['title'];
    }
    /*判断是否能找到此产品，找不到则跳出*/
    empty($r_lid)?exit('{"status":"fail","msg":"未找到产品"}'):'';
    foreach($r_lid as $key_one => $val_one)
    {
        /*通过上面查找到的景区的lid，循环搜索其中的票tid*/
        $sql_res = "SELECT id,title,landid FROM uu_jq_ticket WHERE landid=".$val_one['id']."";//搜索景区的id
        $GLOBALS['le']->query($sql_res);
        while($row_res=$GLOBALS['le']->fetch_assoc()){
            $r_tid[$row_res['id']][$row_res['landid']]['id']     = $row_res['id'];//票的tid
            $r_tid[$row_res['id']][$row_res['landid']]['title']  = $row_res['title'];
            $r_tid[$row_res['id']][$row_res['landid']]['landid'] = $row_res['landid'];//景区的lid
        }
    }
    /*判断是否能找到此产品的票tid，找不到则跳出*/
    empty($r_tid)?exit('{"status":"fail","msg":"未找到产品"}'):'';
    foreach($r_tid as $key_two => $val_two)
    {
        $cou=count($r_tid);
        /*通过一个个值的循环，搜索各个票类的信息，包括字段id,aid,mid,tid,tnum,torder,money*/
        foreach($val_two as $key_seven => $val_seven){
            $tid[$key_two][$key_two]=$r_tid[$key_two][$key_seven]['id'];
            $query   = array(
                'target'   =>$target,
                'tid'      =>$tid[$key_two][$key_two],
                'endtime'  =>$endtime,
                'begintiem'=>$begintiem
            );
            /*连接另外一个服务器，搜索数据*/
            $que    = curr($query);
            /*将各个循环的数值放在数组$json中*/
            $json[$r_tid[$key_two][$key_seven]['id']]   = json_decode($que,true);
            /*判断$json数组的数量与票类的id是否小于，小于则说明tid的信息没有全部被全部搜索出来，继续循环操作，满足条件时继续以下的操作*/
            if(count($json)<$cou) continue;
            /*删除数组中为空的数组*/
            $json_fit=array_filter($json);

            /*显示原始供应商、、一级供销商的情况*/
            if($target==2)
            {

                foreach($json_fit as $key_fit=>$val_fit)
                {
                    /*分别搜索分销商*/
                    foreach($val_fit as $key_eight => $val_eight)
                    {
                        $aid[$key_eight]=$val_eight['aid'];
                        /*搜索分销商的名称*/
                        $sql_re       = "SELECT id,dname FROM pft_member WHERE id=$aid[$key_eight] ";
                        $GLOBALS['le']->query($sql_re);
                        while( $row_re=$GLOBALS['le']->fetch_assoc())
                        {
                            /*得到供应商名称*/
                            $dname[$key_seven][$aid[$key_eight]]  = $row_re['dname'];
                            $tnum[$key_seven][$aid[$key_eight]]  += $val_fit[$key_eight]['tnum'];//分别得到数值
                            $torder[$key_seven][$aid[$key_eight]]+= $val_fit[$key_eight]['torder'];
                            $money[$key_seven][$aid[$key_eight]] += $val_fit[$key_eight]['money'];
                        }
                    }
                }
            }
            elseif($target==1){
            foreach($json_fit as $key_fit=>$val_fit)
            {
                /*分别搜索分销商*/
                foreach($val_fit as $key_eight => $val_eight)
                {
                    $aid[$key_eight]=$val_eight['mid'];
                    /*搜索分销商的名称*/
                    $sql_re = "SELECT id,dname FROM pft_member WHERE id=$aid[$key_eight] limit 100 ";
                    $GLOBALS['le']->query($sql_re);
                    while( $row_re=$GLOBALS['le']->fetch_assoc())
                    {
                        /*得到供应商名称*/
                        $dname[$key_seven][$aid[$key_eight]]  = $row_re['dname'];
                        $tnum[$key_seven][$aid[$key_eight]]  += $val_fit[$key_eight]['tnum'];//分别得到数值
                        $torder[$key_seven][$aid[$key_eight]]+= $val_fit[$key_eight]['torder'];
                        $money[$key_seven][$aid[$key_eight]] += $val_fit[$key_eight]['money'];
                    }
                }
            }
        }
        }
    }

    empty($json_fit)?exit('{"status":"fail","msg":"此产品在此期间未购买物品"}'):'';
    empty($dname)?exit('{"status":"fail","msg":"此产品在此期间未购买物品"}'):'';
    $return=array();
    /*通过供应商的循环得到返回值*/
    foreach($dname as $ke=>$va)
    {
        foreach($va as $k =>$v){
            $return[$k]['p_type']   = $r_lid[$id]['p_type'];
            $return[$k]['jtype']    = $r_lid[$id]['jtype'];
            $return[$k]['title']    = $r_lid[$id]['title'];
            $return[$k]['dname']    = $va[$k];
            $return[$k]['tnums']    = $tnum[$ke][$k];
            $return[$k]['moneys']   = $money[$ke][$k];
            $return[$k]['torders']  = $torder[$ke][$k];
        }
    }
    echo json_encode($return);

}/*没有输入产品名称及搜索一级供应商*/
else if(empty($title) && $target==2){
    $query_tid   = array(
        'target'   =>$target,
        'endtime'  =>$endtime,
        'begintiem'=>$begintiem
    );
    /*连接另外一个服务器，搜索数据*/
    $que_tid  = curr($query_tid);
    $json_tid = json_decode($que_tid,true);
    $sql = 'SELECT id,landid FROM uu_jq_ticket WHERE id in('.$json_tid['tid'].')';//id 是tid
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc())
    {
        $r_tid[$row['id']] =$row['landid'];
        $lid_str .= $row['landid'].',';
    }
    $lid_str = rtrim($lid_str,',');//landid

    $sql= "SELECT id,p_type,jtype,apply_did,title FROM uu_land WHERE id in($lid_str)";//landid

    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc())//landid联系在一起
    {
        foreach($r_tid as $k=>$v){//$r_tid   tid联系在一起
            if($v == $row['id']){
                $r_lid[$k] = array(
                    'p_type'   =>$p_type[$row['p_type']],
                    'jtype'    =>$row['jtype'],
                    'apply_did'=>$row['apply_did'],
                    'title'    =>$row['title'],
                    'lid'      =>$row['id']
                );
                break;
            }
        }
    }
    empty($r_lid)?exit('{"status":"fail","msg":"未找到产品"}'):'';
    $sql_re   = "SELECT id,dname FROM pft_member WHERE id in({$json_tid['aid']})";//搜索分销商的名称aid
    $GLOBALS['le']->query($sql_re);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $dname[$row['id']] = array(//aid联系在一起
            'dname'=>$row['dname'],
        );
    }
    foreach($json_tid as $k=>$v){
        /*过滤无用数据*/
        if($k=="tid") continue;
        elseif($k=="aid")continue;
        if(empty($r_lid[$v['tid']])) continue;
        /*组成返回数据*/
        $return[(int)($v['aid'].$r_lid[$v['tid']]['lid'])]['tnums']   += $v['tnum'];
        $return[(int)($v['aid'].$r_lid[$v['tid']]['lid'])]['moneys']  += $v['money'];
        $return[(int)($v['aid'].$r_lid[$v['tid']]['lid'])]['torders'] += $v['torder'];
        $return[(int)($v['aid'].$r_lid[$v['tid']]['lid'])]['title']   = $r_lid[$v['tid']]['title'];
        $return[(int)($v['aid'].$r_lid[$v['tid']]['lid'])]['jtype']   = $r_lid[$v['tid']]['jtype'];
        $return[(int)($v['aid'].$r_lid[$v['tid']]['lid'])]['p_type']  = $r_lid[$v['tid']]['p_type'];
        $return[(int)($v['aid'].$r_lid[$v['tid']]['lid'])]['dname']   = $dname[$v['aid']]['dname'];
    }
    echo json_encode($return);
    exit;
}
    /*搜索产品标题未填写且所有分销商信息显示出来*/
else if(empty($target)||$target==1){
    $query_re   = array(
        'target'   =>$target,
        'endtime'  =>$endtime,
        'begintiem'=>$begintiem
    );
    /*连接另外一个服务器，搜索数据*/
    $que_re  = curr($query_re);
    /*得到一定期限内产品的票的信息及tid*/
    $json_re = json_decode($que_re,true);
    /*搜索各个tid的landid*/
    empty($json_re['tid'])?exit('{"status":"fail","msg":"此产品在此期间未购买物品"}'):'';

    $sql = 'SELECT id,title,landid FROM uu_jq_ticket WHERE id in('.$json_re['tid'].')';//id 是tid
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc())
    {
        $row_t[$row['id']] =$row['landid'];
        $lid_str .= $row['landid'].',';
    }
    $lid_str = rtrim($lid_str,',');
    /*通过landid搜索景区的各个信息*/
    $sql= "SELECT id,p_type,jtype,apply_did,title FROM uu_land WHERE id in($lid_str)";//landid
    $GLOBALS['le']->query($sql);
    while($row=$GLOBALS['le']->fetch_assoc())//landid联系在一起
    {
        foreach($row_t as $k=>$v){//$row_t   tid联系在一起
            if($v == $row['id']){
                $row_l[$k] = array(
                    'p_type'   =>$p_type[$row['p_type']],
                    'jtype'    =>$row['jtype'],
                    'apply_did'=>$row['apply_did'],
                    'title'    =>$row['title'],
                    'lid'      =>$row['id']
                );
                break;
            }
        }
    }
    empty($row_l)?exit('{"status":"fail","msg":"未找到产品"}'):'';
    $sql_re   = "SELECT id,dname FROM pft_member WHERE id in({$json_re['aid']})";//搜索分销商的名称aid
    $GLOBALS['le']->query($sql_re);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $dname[$row['id']] = array(//aid联系在一起
            'dname'=>$row['dname'],
        );
    }
    foreach($json_re as $k=>$v){
        /*过滤数据*/
        if($k=="tid") continue;
        elseif($k=="aid")continue;
        if(empty($row_l[$v['tid']])) continue;
        $return[(int)($v['aid'].$row_l[$v['tid']]['lid'])]['tnums']   += $v['tnum'];
        $return[(int)($v['aid'].$row_l[$v['tid']]['lid'])]['moneys']  += $v['money'];
        $return[(int)($v['aid'].$row_l[$v['tid']]['lid'])]['torders'] += $v['torder'];
        $return[(int)($v['aid'].$row_l[$v['tid']]['lid'])]['title']   = $row_l[$v['tid']]['title'];
        $return[(int)($v['aid'].$row_l[$v['tid']]['lid'])]['jtype']   = $row_l[$v['tid']]['jtype'];
        $return[(int)($v['aid'].$row_l[$v['tid']]['lid'])]['p_type']  = $row_l[$v['tid']]['p_type'];
        $return[(int)($v['aid'].$row_l[$v['tid']]['lid'])]['dname']   = $dname[$v['aid']]['dname'];

    }
    echo json_encode($return);
    exit;
}else if($target==3 && !empty($title))
{
    /*通过输入的产品名称搜索数据库得到产品的信息*/
    $sql= "SELECT id,p_type,jtype,apply_did,title FROM uu_land WHERE title='$title' limit 1";
    $GLOBALS['le']->query($sql);
    /*搜索显示景区的信息，键值为景区id即lid*/
    while($row=$GLOBALS['le']->fetch_assoc())
    {
        $id=$row['id'];
        $r_lid[$row['id']]['id']       =$row['id'];
        $r_lid[$row['id']]['p_type']   =$p_type[$row['p_type']];
        $r_lid[$row['id']]['jtype']    =$row['jtype'];
        $r_lid[$row['id']]['apply_did']=$row['apply_did'];
        $r_lid[$row['id']]['title']    =$row['title'];
    }
    /*判断是否能找到此产品，找不到则跳出*/
    empty($r_lid)?exit('{"status":"fail","msg":"未找到产品"}'):'';
    foreach($r_lid as $key_one => $val_one)
    {
        /*通过上面查找到的景区的lid，循环搜索其中的票tid*/
        $sql_res = "SELECT id,title,landid,apply_did FROM uu_jq_ticket WHERE landid=".$val_one['id']."";//搜索景区的id
        $GLOBALS['le']->query($sql_res);
        while($row_res=$GLOBALS['le']->fetch_assoc()){
            $r_tid[$row_res['id']][$row_res['landid']]['id']     = $row_res['id'];//票的tid
            $r_tid[$row_res['id']][$row_res['landid']]['title']  = $row_res['title'];
            $r_tid[$row_res['id']][$row_res['landid']]['landid'] = $row_res['landid'];//景区的lid
            $aid=$row_res['apply_did'];//景区的供应商
            $tid.=$row_res['id'].',';
        }
    }
    $tid=rtrim($tid,',');
    /*判断是否能找到此产品的票tid，找不到则跳出*/
    empty($r_tid)?exit('{"status":"fail","msg":"未找到产品"}'):'';

    $query   = array(
        'target'   =>$target,
        'tid'      =>$tid,
        'endtime'  =>$endtime,
        'begintiem'=>$begintiem
    );
    /*连接另外一个服务器，搜索数据*/
    $que    = curr($query);
    /*将各个循环的数值放在数组$json中*/
    $json  = json_decode($que,true);
    $sql_r="SELECT dname,id From pft_member WHERE id in({$json['mid']})";
    $GLOBALS['le']->query($sql_r);
    while($row = $GLOBALS['le']->fetch_assoc()){
        $dname[$row['id']] = $row;
    }
    foreach($json as $json_key => $json_val)
    {
        if($json_key=='mid') continue;
        if($json_key=='tid') continue;
        foreach($json_val as $v)
        {
            $return[(int)($v['mid'].$r_tid[$v['tid']][$id])]['tnums']   +=$v['tnum'];
            $return[(int)($v['mid'].$r_tid[$v['tid']][$id])]['torders'] +=$v['torder'];
            $return[(int)($v['mid'].$r_tid[$v['tid']][$id])]['moneys']  +=$v['money'];
            $return[(int)($v['mid'].$r_tid[$v['tid']][$id])]['title']    =$r_lid[$id]['title'];
            $return[(int)($v['mid'].$r_tid[$v['tid']][$id])]['jtype']    =$r_lid[$id]['jtype'];
            $return[(int)($v['mid'].$r_tid[$v['tid']][$id])]['p_type']   =$r_lid[$id]['p_type'];
            $return[(int)($v['mid'].$r_tid[$v['tid']][$id])]['dname']    =$dname[$v['mid']]['dname'];

        }
    }
    echo json_encode($return);
    exit;
}/*没有输入产品名称且显示末级供销商*/
else if(empty($title) && $target==3)
{
    $query_tid   = array(
        'target'   =>$target,
        'endtime'  =>$endtime,
        'begintiem'=>$begintiem
    );
    /*连接另外一个服务器，搜索数据*/
    $que_tid  = curr($query_tid);
    $json_tid = json_decode($que_tid,true);

    $sql_tid = 'SELECT id,landid FROM uu_jq_ticket WHERE id in('.$json_tid['tid'].')';//id 是tid
    $GLOBALS['le']->query($sql_tid);
    while($row=$GLOBALS['le']->fetch_assoc())
    {
        $row_t[$row['id']] =$row['landid'];
        $lid_str .= $row['landid'].',';
    }
    $lid_str = rtrim($lid_str,',');//landid
    $sql_lid= "SELECT id,p_type,jtype,apply_did,title FROM uu_land WHERE id in($lid_str)";//landid

    $GLOBALS['le']->query($sql_lid);
    while($row=$GLOBALS['le']->fetch_assoc())//landid联系在一起
    {
        foreach($row_t as $k=>$v){//$row_t   tid联系在一起
            if($v == $row['id']){
                $r_lid[$k] = array(
                    'p_type'   =>$p_type[$row['p_type']],
                    'jtype'    =>$row['jtype'],
                    'apply_did'=>$row['apply_did'],
                    'title'    =>$row['title'],
                    'lid'      =>$row['id']
                );
                break;
            }
        }
    }
    empty($r_lid)?exit('{"status":"fail","msg":"未找到产品"}'):'';
    $sql_re   = "SELECT id,dname FROM pft_member WHERE id in({$json_tid['mid']})";//搜索分销商的名称aid
    $GLOBALS['le']->query($sql_re);
    while($row=$GLOBALS['le']->fetch_assoc()){
        $dname[$row['id']] = array(//aid联系在一起
            'dname'=>$row['dname'],
        );
    }
    foreach($json_tid as $json_key=>$json_val){
        if($json_key=='mid') continue;
        if($json_key=='tid') continue;
        foreach($json_val as $v)
        {
            if(empty($r_lid[$v['tid']])) continue;
            $return[(int)($v['mid'].$r_lid[$v['tid']]['lid'])]['tnums']   +=$v['tnum'];
            $return[(int)($v['mid'].$r_lid[$v['tid']]['lid'])]['torders'] +=$v['torder'];
            $return[(int)($v['mid'].$r_lid[$v['tid']]['lid'])]['moneys']  +=$v['money'];
            $return[(int)($v['mid'].$r_lid[$v['tid']]['lid'])]['title']    =$r_lid[$v['tid']]['title'];
            $return[(int)($v['mid'].$r_lid[$v['tid']]['lid'])]['jtype']    =$r_lid[$v['tid']]['jtype'];
            $return[(int)($v['mid'].$r_lid[$v['tid']]['lid'])]['p_type']   =$r_lid[$v['tid']]['p_type'];
            $return[(int)($v['mid'].$r_lid[$v['tid']]['lid'])]['dname']    =$dname[$v['mid']]['dname'];

        }
    }
    echo json_encode($return);
    exit;
}
function curr($query){
    //$query['token'] = md5($query['btime'].'7667659191929397103470329788807862402627565924638280479623896');
    $url = "http://s.12301.cc/pft/order_report/distribution.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, true );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 1);
//    return json_decode(curl_exec($ch),true);
    return curl_exec($ch);
}
